/******************************************************************************

	COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2010.

	This file is part of Maxsi Window Sample.

	Maxsi Window Sample is free software: you can redistribute it and/or modify
	it under the terms of the GNU Lesser General Public License as published by
	the Free Software Foundation, either version 3 of the License, or (at your
	option) any later version.

	Maxsi Window Sample is distributed in the hope that it will be useful, but
	WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
	or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
	License for more details.

	You should have received a copy of the GNU Lesser General Public License
	along with Maxsi Window Sample. If not, see <http://www.gnu.org/licenses/>.

	MaxsiWindowSample
	An example on how to use the Maxsi Window System for simple things.

	Main.cpp
	The example code on how to correctly create a simple Window and do simple
	things using the Maxsi Window System. This example file also demonstrates
	how to handle error conditions gracefully.

******************************************************************************/

#include "MaxsiLibrary.h"

MESTR*	ApplicationName		=	_MESTR("The Maxsi Window Sample");

MaxsiWindowSystem*	WindowSystem	=	NULL;
MaxsiWindow*		MainWindow		=	NULL;
MaxsiWindow*		New				=	NULL;
MaxsiWindow*		Next			=	NULL;
MaxsiWindow*		Assimilate		=	NULL;
MaxsiWindow*		ProgressBar		=	NULL;

MaxsiError OnEvent(MaxsiWindowEvent* Event, BYTE** UserData)
{
	if ( Event->Owner == New && Event->Type == MAXSI_EVENT_PRESSED )
	{
		ProgressBar->SetProgress(0.30f, MAXSI_PROGRESS_NORMAL);
		MainWindow->SetProgress(0.30f, MAXSI_PROGRESS_NORMAL);
		Assimilate->SetText(_MESTR("Assimilate"));
		ProgressBar->Move(ProgressBar->Left(), ProgressBar->Top() - 5, ProgressBar->Width(), ProgressBar->Height());
	}
	if ( Event->Owner == Next && Event->Type == MAXSI_EVENT_PRESSED )
	{
		Assimilate->Become(Next);
		Next->Move(Next->Left()-170, Next->Top(), Next->Width(), Next->Height());
		ProgressBar->SetProgress(0.55f, MAXSI_PROGRESS_PAUSED);
		MainWindow->SetProgress(0.55f, MAXSI_PROGRESS_PAUSED);
		Assimilate->SetText(_MESTR("Integrate"));
	}
	if ( Event->Owner == Assimilate && Event->Type == MAXSI_EVENT_PRESSED )
	{
		ProgressBar->SetProgress(0.85f,  MAXSI_PROGRESS_INDETERMINATE);
		MainWindow->SetProgress(0.85f,  MAXSI_PROGRESS_INDETERMINATE);
		Assimilate->SetText(_MESTR("Segregate"));
		ProgressBar->Move(ProgressBar->Left(), ProgressBar->Top() + 5, ProgressBar->Width(), ProgressBar->Height());
	}

	return MAXSI_ERROR_SUCCESS;
}

int CreateControls()
{
	// Create a button that does nothing.
	New				=	MainWindow->CreateChild(_MESTR("Button"), 150, 30, 100, 100, _MESTR("New"));
	Next			=	MainWindow->CreateChild(_MESTR("Button"), 150, 30, 270, 100, _MESTR("Next"));
	Assimilate		=	MainWindow->CreateChild(_MESTR("Button"), 150, 30, 440, 100, _MESTR("Assimilate"));
	ProgressBar		=	MainWindow->CreateChild(_MESTR("ProgressBar"), 600, 25, 100, 150, NULL);

	ProgressBar->SetProgress(0.7f, MAXSI_PROGRESS_ERROR);

	if ( New == NULL || ProgressBar == NULL ) { return 0; }

	return 1;
}

int main(int argc, char** argv)
{
	// Get an instance of the local Maxsi Window System used on this platform.
	WindowSystem	=	(MaxsiWindowSystem*)CreateInterface("MaxsiWindowSystem");

	if ( WindowSystem == NULL )
	{
		CriticalMsgBox(NULL, 0, _MESTR("Could not create an instance of the Maxsi Window System."), ApplicationName);
		return 1;	
	}

	// Initialize the Maxsi Window System.
	if ( WindowSystem->Initialize(&argc, &argv) == 0 )
	{
		CriticalMsgBox(NULL, 0, _MESTR("Could not intialize the Maxsi Window System."), ApplicationName);	
		DeleteInterface(WindowSystem);	
		return 1;
	}

	// Create the main Window
	MainWindow	=	WindowSystem->CreateWindow(NULL, _MESTR("Window"), 800, 600, MAXSI_WINDOW_CENTER_X, MAXSI_WINDOW_CENTER_Y, ApplicationName);

	if ( MainWindow == NULL )
	{
		CriticalMsgBox(NULL, 0, _MESTR("Could not create the main window."), ApplicationName);	
		DeleteInterface(WindowSystem);	
		return 1;		
	}

	// Set up the callback function that handles all events on behalf of MainWindow.
	MainWindow->SetEventHandler(OnEvent, NULL);

	// Call the function above that creates all the controls!
	if ( CreateControls() == 0 )
	{
		CriticalMsgBox(NULL, 0, _MESTR("Could not create the controls on the main window."), ApplicationName);
		WindowSystem->DeleteWindow(MainWindow);
		DeleteInterface(WindowSystem);	
		return 1;	
	}	

	// Now that we are done adding controls, it is time to show the window.
	MainWindow->Show();

	// Run the main event loop.
	int	Result = WindowSystem->MainLoop();

	// Clean up!	
	WindowSystem->DeleteWindow(MainWindow);
	DeleteInterface(WindowSystem);

	return Result;
}


